package com.sto.stoapp.activity.administrator.hardware;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;

import com.sto.stoapp.StoApplication;
import com.sto.stoapp.activity.R;
import com.sto.stoapp.broadcast.HomeBroadAction;
import com.sto.stoapp.listener.OnBackClickListener;
import com.sto.stoapp.widget.BaseDialog;
import com.sto.stoapp.widget.StoHomeActivity;

import sto.android.app.StoPdaKeyEvent;

/**
 * usb通信
 * 
 * @author mgf
 * 
 */
public class AdminUsbConnectionActivity extends StoHomeActivity {

	public static final String ACTION_USB_STATE = "android.hardware.usb.action.USB_STATE";

	public static final String ACTION_USB_ACCESSORY_ATTACHED = "android.hardware.usb.action.USB_ACCESSORY_ATTACHED";

	public static final String ACTION_USB_ACCESSORY_DETACHED = "android.hardware.usb.action.USB_ACCESSORY_DETACHED";

	public static final String USB_CONNECTED = "connected";

	public static final String USB_FUNCTION_ENABLED = "enabled";

	public static final String USB_FUNCTION_ADB = "adb";

	private UsbBroadCast usbBroadReceiver;

	private boolean isConnected = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.admin_usb_connection);
		this.setTitleBottom(R.string.usb_connection,
				HomeBroadAction.ADMINISTRATOR_BROAD);
		usbBroadReceiver = new UsbBroadCast();
		IntentFilter intentFilter = new IntentFilter();
		intentFilter.addAction(ACTION_USB_STATE);
		intentFilter.addAction(ACTION_USB_ACCESSORY_ATTACHED);
		intentFilter.addAction(ACTION_USB_ACCESSORY_DETACHED);

		registerReceiver(usbBroadReceiver, intentFilter);
	}

	@Override
	public void onBackPressed() {
		// super.onBackPressed();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		unregisterReceiver(usbBroadReceiver);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (StoApplication.getStoJni().getEventFuction(keyCode) == StoPdaKeyEvent.KEYCODE_DPAD_CENTER) {
			justDialog();
		}
		return super.onKeyDown(keyCode, event);
	}

	private void justDialog() {

		final BaseDialog dialog = new BaseDialog(this);
		// 测试失败
		if (!isConnected) {
			dialog.setTitles(getString(R.string.usb_connection_failure));
		}
		// 测试成功
		else {
			dialog.setTitles(getString(R.string.usb_connection_success));
		}
		dialog.show();
		if (isConnected) {
			dialog.getvCanncel().setVisibility(View.GONE);
		}
		dialog.getvMessage().setVisibility(View.GONE);
		dialog.getvPresentation().setVisibility(View.GONE);
		dialog.setOnBackClickListener(new OnBackClickListener() {

			@Override
			public void onBackClickListener(boolean res) {
				dialog.dismiss();
				if (res) {
					if (isConnected) {
						finish();
					}
				} else {
					finish();
				}
			}
		});

	}

	class UsbBroadCast extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
			String action = intent.getAction();
			// Toast.makeText(MainActivity.this, action,
			// Toast.LENGTH_LONG).show();
			if (action.equals(ACTION_USB_STATE)) {
				Bundle extras = intent.getExtras();
				isConnected = extras.getBoolean(USB_CONNECTED);
			}
		}

	}
	
}
